package com.charging.service.impl;

import com.charging.entity.Announcement;
import com.charging.dao.AnnouncementDao;
import com.charging.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;



@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    public enum Status {
        PUBLISHED,
        DRAFT,
        ARCHIVED
    }

    private static final Enum PUBLISHED = Status.PUBLISHED;
    @Autowired
    private AnnouncementDao announcementDao;

    //  @Override
    //public List<Announcement> getAllValidAnnouncements() {
    //      return announcementDao.selectValidAnnouncements();
    //   }

    @Override
    public List<Announcement> getAllValidAnnouncements() {
        return announcementDao.selectValidAnnouncements();
    }

    @Override
    public Announcement getAnnouncementById(Long id) {
        return announcementDao.selectById(id);
    }

    @Override
    public void add(Announcement announcement) {
        // 设置默认状态为已发布
        if (announcement.getStatus() == null) {
            announcement.setStatus(Announcement.Status.PUBLISHED);
        }

        // 设置创建时间和更新时间
        Date now = new Date();
        announcement.setCreateTime(now);
        announcement.setUpdateTime(now);

        announcementDao.insert(announcement);
    }

    @Override
    public void updateAnnouncement(Announcement announcement) {
        // 更新时间自动设置为当前时间（数据库触发器会处理）
        announcementDao.update(announcement);
    }

    @Override
    public void delete(Long id) {
        announcementDao.delete(id);
    }

    @Override
    public List<Announcement> getAnnouncementsByType(String type) {
        return announcementDao.selectByType(type);
    }

    @Override
    public void updateAnnouncementStatus(Long id, String status) {
        announcementDao.updateStatus(id, status);
    }
}